using System.Threading.Tasks;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using MinimalistFoodStreet.BackEnd.Infrastructure.Attributes;
using MinimalistFoodStreet.BackEnd.Infrastructure.Dtos.Request.Tables;
using MinimalistFoodStreet.BackEnd.Infrastructure.Dtos.Response;
using MinimalistFoodStreet.BackEnd.Server.IServer.Tables;

namespace MinimalistFoodStreet.BackEnd.Api.Api.Controllers.Tables
{

	[CheckLogin(new CheckLoginRoles[] {CheckLoginRoles.administrators})]
	public class SuppliersController : BaseController 
	{
		private readonly ISuppliersServer _iSuppliersServer;

		public SuppliersController (ISuppliersServer iSuppliersServer)
		{
			_iSuppliersServer = iSuppliersServer;
		}
		
		/// <summary>
		/// 添加
		/// </summary>
		/// <param name="dto"></param>
		/// <returns></returns>
		[HttpPost]
		[ActionLog("添加供应商")]
		public async Task<IActionResult> Add(RequestCreateSuppliersDto dto)
		{
			return Send(await _iSuppliersServer.AddAsync(dto));
		}

		/// <summary>
		/// 修改
		/// </summary>
		/// <param name="dto"></param>
		/// <returns></returns>
		[HttpPost]
		[ActionLog("修改供应商")]
		public async Task<IActionResult> Update(RequestUpdateSuppliersDto dto)
		{
			return Send(await _iSuppliersServer.UpdateAsync(dto));
		}

		/// <summary>
		/// 删除
		/// </summary>
		/// <param name="id"></param>
		/// <returns></returns>
		[HttpPost]
		[ActionLog("删除供应商")]
		public async Task<IActionResult> Delete(RequestDeleteSuppliersDto dto)
		{
			return Send(await _iSuppliersServer.DeleteAsync(dto));
		}

		/// <summary>
		/// 详情
		/// </summary>
		/// <param name="id"></param>
		/// <returns></returns>
		[HttpGet]
		public async Task<IActionResult> Detail(int id)
		{
			return Send(await _iSuppliersServer.DetailAsync(id));
		}
		
		/// <summary>
		/// 分页
		/// </summary>
		/// <param name="page">当前是第几页</param>
		/// <param name="limit">一页有多少条数据</param>
		/// <param name="key">要查询的关键字</param>
		/// <returns></returns>
		[HttpGet]
		public async Task<IActionResult> Pagination(int page,int limit,string? key)
		{
			return Send(await _iSuppliersServer.PaginationAsync(page,limit,key));
		}
		/// <summary>
		/// 获取自动完成选项根据店铺名称
		/// </summary>
		/// <param name="shopName"></param>
		/// <returns></returns>
		[HttpGet]
        public async Task<IActionResult> GetOptionByShopName(string shopName)
		{
			return Send(await _iSuppliersServer.GetOptionByShopName(shopName));
		}
    }
}
	